<template>
  <base-breadcrumb class="allPlan">
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <!-- <title-name title="总包合同管理" /> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        row-key="id"
        :export-permission="{ select: 'lcm_007_exp_03', all: 'lcm_007_exp_03' }"
        :export-config="{ use: true, select: '总包合同选中', all: '总包合同全部' }"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
          slot="prefix"
        >
          <a-button
            v-btn-permission="'lcm_007_add_02'"
            type="primary"
            icon="plus"
            @click="handleEdit(null, 1, undefined)"
          >
            新增
          </a-button>
          <a-button
            v-btn-permission="'lcm_007_cha_04'"
            type="primary"
            icon="plus"
            @click="handleChange(null, 1, 10)"
          >
            合同变更
          </a-button>
        </template>
        <span
          slot="index"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
          slot="contractTypeStr"
          slot-scope="text, record"
        >
          {{ record.baseChangeStatus ? record.contractChangeName : record.contractTypeStr }}
        </template>
        <template
          slot="branchComReviewStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.branchComReview"
            :text="text"
          />
        </template>

        <template
          slot="comAuditReviewStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.comAuditReview"
            :text="text"
          />
        </template>
        <template
          slot="baseChangeStatusStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.baseChangeStatus"
            :text="text"
          />
        </template>
        <template
          slot="comReviewStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.comReview"
            :text="text"
          />
        </template>
        <template
          slot="authorizationStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.authorization"
            :text="text"
          />
        </template>
        <template
          slot="sealApplyStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.sealApply"
            :text="text"
          />
        </template>
        <template
          slot="costAnalysisStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.costAnalysis"
            :text="text"
          />
        </template>
        <template
          slot="fundPlanTableStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.fundPlanTable"
            :text="text"
          />
        </template>
        <template
          slot="majorRisksStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.majorRisks"
            :text="text"
          />
        </template>
        <template
          slot="riskReviewStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.riskReview"
            :text="text"
          />
        </template>

        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-if="!record.baseChangeStatus"
              v-btn-permission="'lcm_007_loo_01'"
              @click="handleEdit(record, 3)"
            >查看</a>
            <a
              v-if="record.baseChangeStatus"
              @click="handleChange(record, 3, 10)"
            >
              <!--合同变更-->查看
            </a>
            <template  v-if=" record.baseChangeStatus === 'DRAFT' || (record.branchComReview === 'DRAFT' && record.comAuditReview === 'DRAFT')">
            <a
              v-if="
                record.baseChangeStatus &&
                  record.baseChangeStatus != 'SUBMITTED' &&
                  record.baseChangeStatus != 'PAST'&&
                  record.creatorId == id&&hideBuquBtn()
              "
              @click="() => handleChange(record, 2, 10)"
            >
              <!--合同变更-->编辑
            </a>
            <a
              v-if="
                record.branchComReview &&
                  record.branchComReview != 'SUBMITTED' &&
                  record.branchComReview != 'PAST' &&
                  record.creatorId == id&&hideBuquBtn()
              "
              @click="() => handleEdit(record, 10, undefined)"
            >编辑</a>
            <a
              v-if="
                (record.branchComReview == 'DRAFT' || record.baseChangeStatus == 'DRAFT') &&
                  record.creatorId == id&&hideBuquBtn()
              "
              @click.stop="() => handleDel(record.baseId)"
            >删除</a>
            </template>
            <!--<span-->
            <!--v-if="isShowBaseChangeBtn(record)"-->
            <!--v-btn-permission="'lcm_004_cha_05'"-->
            <!--@click="handleEdit(record, 2, undefined)"-->
            <!--&gt;变更</span>-->
            <!--                        <span @click="handleEdit(record, 2,undefined)">变更</span>-->

            <template v-else>
            <template>
              <span
                v-if="isShowBaseOptions(record, record.comReview)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_rev_05'"
              >
                <a @click="handleEdit(record, 2, 3)">评审汇总 </a>
              </span>
            </template>
            <template>
              <span
                v-if="isShowBaseOptions(record, record.authorization)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_aut_06'"
              >
                <a @click="handleEdit(record, 2, 4)">授权 </a>
              </span>
            </template>
            <template>
              <span
                v-if="isShowBaseOptions(record, record.sealApply, 'sealApply')&&hideBuquBtn()"
                v-btn-permission="'lcm_007_sea_07'"
              >
                <a @click="handleEdit(record, 2, 5)">用印 </a>
              </span>
            </template>
            <template>
              <span
                v-if="isShowBaseOptions(record, record.costAnalysis)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_cha_08'"
              >
                <a @click="handleEdit(record, 2, 6)">成本分析 </a>
              </span>
            </template>
            <template>
              <span
                v-if="isShowBaseOptions(record, record.fundPlanTable)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_pla_09'"
              >
                <a @click="handleEdit(record, 2, 7)">资金策划</a>
              </span>
            </template>
            <template>
              <a
                v-if="isShowBaseOptions(record, record.majorRisks)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_ris_10'"
              >
                <a @click="handleEdit(record, 2, 8)">重大风险</a>
              </a>
            </template>
            <template>
              <a
                v-if="isShowBaseOptions(record, record.riskReview)&&hideBuquBtn()"
                v-btn-permission="'lcm_007_rre_11'"
              >
                <!--状态为“审核中”或“审核通过”，不显示该按钮-->
                <a @click="handleEdit(record, 2, 9)">风险复核</a>
              </a>
            </template>
            <template v-if="record.branchComReview !== 'DRAFT' && record.baseChangeStatus !== 'DRAFT'">
              <a @click="
                  chooseId = record.baseId
                  baseBusinessId = record.baseBusinessId
                  modal.approval = true
                  baseChangeStatus = record.baseChangeStatus ? record.baseChangeStatus : ''
                ">审批意见</a>
              <!-- <span
                @click="
                  chooseId = record.baseId
                  modal.history = true
                "
              >历史记录</span> -->
            </template>
            </template>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
    <approval
      :id="chooseId"
      ref="approval"
      :base-business-id="baseBusinessId"
      :base-change-status="baseChangeStatus"
      title="审批意见"
      :active-tab.sync="modal.activeTab"
      :visible.sync="modal.approval"
    />
    <history
      :id="chooseId"
      ref="history"
      title="历史记录"
      :visible.sync="modal.history"
      @approval="historyApproval"
    />
  </base-breadcrumb>
</template>

<script>
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import approval from './components/approval'
import history from './components/history'
import { Ellipsis } from '@/components'

export default {
  name: 'a' + Date.now(),
  components: { approval, history },
  data() {
    return {
      columns: [
        {
          title: '序号',
          // width: 80,
          // dataIndex: 'index',
          // customRender() {
          //     return arguments[2] + 1
          // },
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '合同名称',
          dataIndex: 'contractName',
          width: 200,
          ellipsis: true
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName',
          width: 150
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          width: 150
        },
        {
          title: '建设单位名称',
          dataIndex: 'otherParty',
          width: 150,
          ellipsis: true
        },
        {
          // 合同类型 \变更合同的变更类型
          title: '合同类型',
          dataIndex: 'contractTypeStr' || 'contractChangeName',
          scopedSlots: { customRender: 'contractTypeStr' },
          width: 200
          // ellipsis:true
        },
        {
          // 合同额(万元) \变更合同的变更合同额(万元)'
          title: '合同额(万元)',
          dataIndex: 'amount',
          width: 150,
          ellipsis: true
        },
        {
          // 合同额(万元) \变更合同的变更合同额(万元)'
          title: '创建人',
          dataIndex: 'creatorName',
          width: 150
        },
        {
          // 合同额(万元) \变更合同的变更合同额(万元)'
          title: '创建时间',
          dataIndex: 'createTime',
          width: 150
        },
        {
          title: '分公司评审状态',
          dataIndex: 'branchComReviewStr',
          scopedSlots: { customRender: 'branchComReviewStr' },
          width: 130
        },
        {
          title: '合同变更状态',
          dataIndex: 'baseChangeStatusStr',
          scopedSlots: { customRender: 'baseChangeStatusStr' },
          width: 150
        },
        {
          title: '公司评审状态',
          dataIndex: 'comAuditReviewStr',
          scopedSlots: { customRender: 'comAuditReviewStr' },
          width: 130
        },
        {
          title: '评审汇总状态',
          dataIndex: 'comReviewStr',
          scopedSlots: { customRender: 'comReviewStr' },
          width: 130
        },
        {
          title: '授权申请状态',
          dataIndex: 'authorizationStr',
          scopedSlots: { customRender: 'authorizationStr' },
          width: 130
        },
        {
          title: '用印申请状态',
          dataIndex: 'sealApplyStr',
          scopedSlots: { customRender: 'sealApplyStr' },
          width: 130
        },
        {
          title: '成本分析状态',
          dataIndex: 'costAnalysisStr',
          scopedSlots: { customRender: 'costAnalysisStr' },
          width: 130
        },
        {
          title: '资金策划状态',
          dataIndex: 'fundPlanTableStr',
          scopedSlots: { customRender: 'fundPlanTableStr' },
          width: 130
        },
        {
          title: '重大风险状态',
          dataIndex: 'majorRisksStr',
          scopedSlots: { customRender: 'majorRisksStr' },
          width: 130
        },
        {
          title: '风险复核状态',
          dataIndex: 'riskReviewStr',
          scopedSlots: { customRender: 'riskReviewStr' },
          width: 130
        },
        {
          title: '操作',
          width: '300px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      searchArr: [
        // 搜索框
        {
          name: '合同名称',
          type: 'input',
          value: 'contractName'
        },
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   type: SelectSubCompany,
        //   valueKey: 'branchComId'
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId',
        //   keyValue: 'branchComId'
        // },
        {
          name: '建设单位名称',
          type: 'input',
          value: 'otherParty'
        },
        {
          name: '合同额(万元)',
          type: 'rangeInput',
          value: 'amount',
          attr: {
            precision: 2
          }
        },
        {
          name: '创建人',
          type: 'input',
          value: 'creatorName'
        },
        {
          name: '创建时间',
          type: 'dateRange',
          value: 'createTime',
          attr: {
            rangeFormat: ['createTimeStart', 'createTimeEnd']
          }
        },
        {
          name: '分公司评审状态',
          value: 'branchComReview',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '公司评审状态',
          value: 'comAuditReview',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '评审汇总状态',
          value: 'comReview',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '授权申请状态',
          value: 'authorization',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '用印申请状态',
          value: 'sealApply',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '成本分析状态',
          value: 'costAnalysis',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '资金策划状态',
          value: 'fundPlanTable',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '重大风险状态',
          value: 'majorRisks',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '合同变更状态',
          value: 'baseChangeStatus',
          type: 'select',
          data: this.STATUS
        },
        {
          name: '风险复核审批状态',
          value: 'riskReview',
          type: 'select',
          data: this.STATUS
        }
      ],
      loadData: (params) => {
        let data = totalPackageContractApi.findListByMain(Object.assign(params, this.queryParam))
        this.baseChangeStatus = data.baseChangeStatus
        return data
      },
      userId: JSON.parse(sessionStorage.getItem('ycloud-user_info'))?.currentPost?.id,
      queryParam: {},
      chooseId: '',
      baseBusinessId: '',
      baseChangeStatus: '',
      id: JSON.parse(sessionStorage.getItem('ycloud-user_info'))?.id,
      modal: {
        approval: false,
        history: false,
        activeTab: null
      }
    }
  },
  mounted: async function () {},
  methods: {
    historyApproval(data) {
      // if (data.activeKey === '1') {
      //     this.chooseId = data.record.baseBusinessId || data.record.baseId
      // } else {
      //     this.chooseId = data.record.businessId
      // }
      this.chooseId = data.record.businessId || data.record.baseBusinessId || data.record.baseId || ''
      this.modal.approval = true
      this.modal.activeTab = data.activeKey
    },
    isShowBaseChangeBtn(record) {
      return record.branchComReview === 'PAST' && record.creatorId === JSON.parse(sessionStorage.getItem('ycloud-user_info')).employee.userId
    },
    isShowBaseOptions(record, status, type = '') {
      if (record?.baseChangeStatus) {
        if (type == 'sealApply' && status != 'SUBMITTED' && status != 'PAST') {
          return true
        }
        return false
      }
      if (status != 'SUBMITTED' && status != 'PAST') {
        return true
      }
      return false
    },
    search(val) {
      this.queryParam = val || {}

      this.$refs.table.getData(null, true)
    },
    handleDel(id) {
      this.$delList2(totalPackageContractApi.deleteBymain, this.search, { ids: [id] })
    },
    handleEdit(record = {}, state, type) {
      this.$router.push({
        path: '/legal/branch-contract/total-package-contract/' + (state === 3 ? 'detail' : 'add'),
        query: {
          id: (record && record.baseId) || '',
          state: state,
          type,
          baseChangeStatus: '2' //合同变更false
        }
      })
    },
    /**
     * 新增旁边的合同变更
     **/
    handleChange(record = {}, state, type) {
      this.$router.push({
        path: '/legal/branch-contract/total-package-contract/' + (state === 3 ? 'detail' : 'add'),
        query: {
          id: (record && record.baseId) || '',
          state: state,
          type,
          baseChangeStatus: '1' //合同变更true
        }
      })
    }
  }
}
</script>
